package com.taiji.dicom4cloud.smart.alarm.manager;


import com.taiji.dicom4cloud.smart.alarm.annoation.HandlerType;
import com.taiji.dicom4cloud.smart.alarm.common.MessageHandler;
import com.taiji.dicom4cloud.smart.alarm.model.Message;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.AnnotationUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author fanxl
 * @Description 策略控制类
 * @Date 2020/11/13
 **/
public class MessageStrategyManager implements ApplicationContextAware, IMessageAlarmService, InitializingBean {
    /**
     * 处理类集合
     */
    private Map<String, MessageHandler> handlerMap = new ConcurrentHashMap<>(16);
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String,Object> beans = applicationContext.getBeansWithAnnotation(HandlerType.class);
        if (beans != null && beans.size() > 0) {
            for (Object serviceBean : beans.values()) {
                HandlerType handlerType = AnnotationUtils.findAnnotation(serviceBean.getClass(),HandlerType.class);
                handlerMap.put(handlerType.value().getType().toLowerCase(),(MessageHandler) serviceBean);
            }
        }
    }
    @Override
    public void afterPropertiesSet(){
        System.out.println();
    }

    /**
     * 统一处理入口
     * @param message
     * @return
     */
    @Override
    public Object handle(Message message) {
        return handlerMap.get(message.getCode().toLowerCase()).handle(message);
    }
}
